package my.hnb.android.common;

import java.util.ArrayList;

import my.hnb.android.constant.SharedPref;
import my.hnb.android.dto.OptionItemDTO;
import my.hnb.android.model.SQLUtils;
import my.hnb.android.ultils.AppUltils;
import my.hnb.android.view.common.Splashscreen;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class AppInfo extends Application{
	public final static String K_LYRIC = "INTENT_DATA"; 
	public final static String K_LINK = "INTENT_SONG";
	
	final String MUSIC_VOL_OTION = "snMusicVol";
	final String MUSIC_LINK_UPDATE = "snMusicLinkUpdate";
	final String MUSIC_TYPE = "snMusicType2";
	public final String VERSION = "1.0";
	public final String PLATFORM_SDK_STRING = android.os.Build.VERSION.SDK;
	public final String PHONE_MODEL = "Android_" + android.os.Build.MODEL;
	public boolean isHaveUpdate = false;
	Context appContext;//application context
	Context activityContext; //activity context
	private static AppInfo instance = null;
	boolean isDebugMode = false;
	
	private ArrayList<OptionItemDTO> arrayMusicTypeOption;
	private ArrayList<OptionItemDTO> arrayMusicSearchOption;
	private ArrayList<OptionItemDTO> arrayMusicVolOption;
	private ArrayList<OptionItemDTO> listLinkUpdate = new ArrayList<OptionItemDTO>();
	public AppInfo(){
	}
	
	public static AppInfo getInstance(){
		if(instance == null){
			instance = new AppInfo();
		}
		return instance;
	}
	
	public Context getAppContext(){
		return this.appContext;
	}
	
	public Context getActivityContext(){
		return activityContext;
	}
	
	public void setActivityContext(Context context) {
		this.activityContext = context;

	}
	
	public void setAppContext(Context context){
		this.appContext = context;
	}
	
	
	public void setDebugMode(boolean isDebug){
		isDebugMode = isDebug;
	}
	
	public boolean isDebugMode(){
		return isDebugMode;
	}
	
	
	public void setMusicSearhOption(ArrayList<OptionItemDTO> optionList) {
		this.arrayMusicSearchOption = optionList;
		SharedPreferences sharedPref = appContext.getSharedPreferences(Splashscreen.PREFS_PRIVATE,
				Context.MODE_PRIVATE);
		Editor prefsEditor = sharedPref.edit();
		prefsEditor.putBoolean(SharedPref.SEARCH_ALL, arrayMusicSearchOption.get(0).isSelected());
		prefsEditor.putBoolean(SharedPref.SEARCH_NAME, arrayMusicSearchOption.get(1).isSelected());
		prefsEditor.putBoolean(SharedPref.SEARCH_MUSICIAN, arrayMusicSearchOption.get(2).isSelected());
		prefsEditor.putBoolean(SharedPref.SEARCH_LYRIC, arrayMusicSearchOption.get(3).isSelected());
		prefsEditor.commit();
	}

	public ArrayList<OptionItemDTO> getMusicSearchOption() {
		if (arrayMusicSearchOption == null) {
			SharedPreferences sharedPref = appContext.getSharedPreferences(Splashscreen.PREFS_PRIVATE,
					Context.MODE_PRIVATE);
			arrayMusicSearchOption = new ArrayList<OptionItemDTO>();
			OptionItemDTO search1 = new OptionItemDTO("Tất cả", sharedPref.getBoolean(SharedPref.SEARCH_ALL, false));
			OptionItemDTO search3 = new OptionItemDTO("Tiêu đề", sharedPref.getBoolean(SharedPref.SEARCH_NAME, true));
			OptionItemDTO search4 = new OptionItemDTO("Nhạc sĩ", sharedPref.getBoolean(SharedPref.SEARCH_MUSICIAN, false));
			OptionItemDTO search5 = new OptionItemDTO("Lời đầu bài hát", sharedPref.getBoolean(SharedPref.SEARCH_LYRIC, false));
			arrayMusicSearchOption.add(search1);
			arrayMusicSearchOption.add(search3);
			arrayMusicSearchOption.add(search4);
			arrayMusicSearchOption.add(search5);
		}
		return this.arrayMusicSearchOption;
	}
	
	
	@SuppressWarnings("unchecked")
	public ArrayList<OptionItemDTO> getLinkUpdate(){
		if(listLinkUpdate == null){
			try {
				Object temp;
				if ((temp = AppUltils.readObject(MUSIC_LINK_UPDATE)) != null) {
					this.listLinkUpdate = (ArrayList<OptionItemDTO>) temp;
				} 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return listLinkUpdate;
	}
	
	public void setListLinkUpdate(ArrayList<OptionItemDTO> listLink){
		if(listLink != null && listLink.size() > 0){
			listLinkUpdate = new ArrayList<OptionItemDTO>();
			for(int i = 0; i < listLink.size(); i++){
				OptionItemDTO item = new OptionItemDTO(listLink.get(i));
				listLinkUpdate.add(item);
			}
			AppUltils.saveObject(listLinkUpdate, MUSIC_LINK_UPDATE);
		}
	}
	
	public void resetListLinkUpdate(){
		listLinkUpdate.clear();
		AppUltils.saveObject(listLinkUpdate, MUSIC_LINK_UPDATE);
	}
	
	public void setMusicTypeOption(ArrayList<OptionItemDTO> optionList) {
		this.arrayMusicTypeOption = optionList;
		AppUltils.saveObject(arrayMusicTypeOption, MUSIC_TYPE);
	}
	
	
	public void setMusicVolOption(ArrayList<OptionItemDTO> optionList) {
		this.arrayMusicVolOption = optionList;
		AppUltils.saveObject(arrayMusicVolOption, MUSIC_VOL_OTION);
	}
	
	public void resetMusicVolOption(){
		try {
			this.arrayMusicVolOption = SQLUtils.getInstance().getVolNew();
			AppUltils.saveObject(arrayMusicVolOption, MUSIC_VOL_OTION);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void resetMusicTypeOption(){
		try {
			this.arrayMusicTypeOption = SQLUtils.getInstance().getMusicType();
			AppUltils.saveObject(arrayMusicTypeOption, MUSIC_TYPE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<OptionItemDTO> getMusicVolOption() {
		if (arrayMusicVolOption == null) {
			Object temp;
			try {
				if ((temp = AppUltils.readObject(MUSIC_VOL_OTION)) != null) {
					this.arrayMusicVolOption = (ArrayList<OptionItemDTO>) temp;
				} else {
					this.arrayMusicVolOption = SQLUtils.getInstance().getVolNew();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return arrayMusicVolOption;
	}
	
	
	@SuppressWarnings("unchecked")
	public ArrayList<OptionItemDTO> getMusicTypeOption(){
		if(arrayMusicTypeOption == null){
			Object temp;
			try {
				if ((temp = AppUltils.readObject(MUSIC_TYPE)) != null) {
					this.arrayMusicTypeOption = (ArrayList<OptionItemDTO>) temp;
				} else {
					this.arrayMusicTypeOption = SQLUtils.getInstance().getMusicType();
					AppUltils.saveObject(arrayMusicTypeOption, MUSIC_TYPE);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return arrayMusicTypeOption;
	}
}
